# -*- coding: utf-8 -*-

"""
职责链模式

模式特点：使多个对象都有机会处理请求，从而避免发送者和接收者的耦合关系。将对象连成链并沿着这条链传递请求直到被处理。

程序实例：请假和加薪等请求发给上级，如果上级无权决定，那么递交给上级的上级。

代码特点：无

"""


class Request(object):
    def __init__(self, tcontent, tnum):
        self.content = tcontent
        self.num = tnum


class Manager(object):
    def __init__(self, temp):
        self.name = temp

    def set_successor(self, temp):
        self.manager = temp

    def get_request(self, req):
        pass


class CommonManager(Manager):
    def get_request(self, req):
        if req.num >= 0 and req.num < 10:
            print "{} handled {:d} request.".format(self.name, req.num)
        else:
            self.manager.get_request(req)


class MajorDomo(Manager):
    def get_request(self, req):
        if req.num >= 10:
            print "{} handled {:d} request.".format(self.name, req.num)

if __name__ == "__main__":
    common = CommonManager("Zhang")
    major = MajorDomo("Lee")
    common.set_successor(major)
    req = Request("rest", 33)
    common.get_request(req)
    req2 = Request("salary", 3)
    common.get_request(req2)